// pages/search/moredata/moredata.js
//获取应用实例
const app = getApp()
const util = require("../../../utils/util.js");
var page = 1;
Page({

    /**
     * 页面的初始数据
     */
    data: {
        searchList: [],
        servList: [],
        bsyList: [],
        // cityname: app.globalData.extConfig.cityname,
        // citycode: app.globalData.extConfig.citycode,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.setData({
            keywords: options.keywords ? decodeURIComponent(options.keywords) : '',
            name: options.name ? decodeURIComponent(options.name) : '',
            citycode: wx.getStorageSync("citycode") ? wx.getStorageSync("citycode") : 'sz'
        })
        wx.setNavigationBarTitle({
            title: decodeURIComponent(this.data.keywords) + "相关" + decodeURIComponent(this.data.name),
        })
        util.getUrl(app.globalData.url).then((res) => {

            this.getServiceData(this.data.keywords);
            this.getinfoData(this.data.keywords, page);
            this.getbsyData(this.data.keywords, page);
        });
    },

    onShow() {
        page = 0;
    },

    // 获取服务搜索结果
    getServiceData(keywords) {
        let serData = {
            action: "tools",
            city: wx.getStorageSync("city"),
            keywords: keywords,
            type: "search",
            version: app.globalData.version,
        };
        util.request('service.php', serData, ).then(res => {

            this.setData({
                servList: res.data.data,
            })
        })
    },

    // 资讯搜索结果
    getinfoData(keywords, page) {
        let searchData = {
            citycode: this.data.citycode,
            action: 'list',
            pagesize: 20,
            keywords: keywords,
            page,
            version: app.globalData.version,

        };
        util.request('index.php', searchData, 'GET').then(res => {
            this.setData({
                searchList: this.data.searchList.concat(res.data),
            })
        })
    },

    // 办事搜索结果
    getbsyData(keywords, page) {
        let searchData = {
            citycode: this.data.citycode == "sz" ? "bsy" : this.data.citycode,
            action: 'list',
            pagesize: 20,
            keywords: keywords,
            bsy: 1,
            page,
            version: app.globalData.version,

        };
        util.request('index.php', searchData, 'GET').then(res => {
            this.setData({
                bsyList: this.data.bsyList.concat(res.data),
            })
        })
    },

    // 工具、服务点击事件
    servBtn(e) {
        let path = e.currentTarget.dataset.path;
        let appid = e.currentTarget.dataset.appid;
        let linkname = e.currentTarget.dataset.title;
        if (path.indexOf("pages") == -1 && path.indexOf("mp.weixin.qq.com") == -1) {
            this.setData({
                showtk: true
            })
            util.navContact(linkname, path)
        } else {
            util.navMethod(path, appid);
        }
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {
        page++;
        this.getinfoData(this.data.keywords, page);
        this.getbsyData(this.data.keywords, page);
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})